home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / term.c < prev    next >
C/C++ Source or Header  |  1993-11-22  |  52KB  |  1,789 lines

  1. /* terminal control module for terminals described by TERMCAP
  2.    Copyright (C) 1985, 1986, 1987, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include "config.h"
  24. #include "termchar.h"
  25. #include "termopts.h"
  26. #include "cm.h"
  27. #undef NULL
  28. #include "lisp.h"
  29. #include "frame.h"
  30. #include "disptab.h"
  31. #include "termhooks.h"
  32. #include "keyboard.h"
  33.  
  34. #ifdef STDC_HEADERS
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #endif
  38. #include "term_p.h"
  39. #include "term_d.h"
  40. #ifdef WINDOWSNT
  41. #include "ntterm_p.h"
  42. #endif
  43. #include "termcap_p.h"
  44. #include "scroll_p.h"
  45. #include "tparam_p.h"
  46. #include "sysdep_p.h"
  47. static _VOID_ highlight_if_desired _P_((void));
  48. static int string_cost_one_line _P_((char *str));
  49. static void calculate_ins_del_char_costs _P_((FRAME_PTR frame));
  50.  
  51. #ifndef max
  52. #define max(a, b) ((a) > (b) ? (a) : (b))
  53. #endif
  54. #ifndef min
  55. #define min(a, b) ((a) < (b) ? (a) : (b))
  56. #endif
  57.  
  58. #define OUTPUT(a) tputs (a, FRAME_HEIGHT (selected_frame) - curY, cmputc)
  59. #define OUTPUT1(a) tputs (a, 1, cmputc)
  60. #define OUTPUTL(a, lines) tputs (a, lines, cmputc)
  61. #define OUTPUT_IF(a) { if (a) tputs (a, FRAME_HEIGHT (selected_frame) - curY, cmputc); }
  62. #define OUTPUT1_IF(a) { if (a) tputs (a, 1, cmputc); }
  63.  
  64. /* Terminal characteristics that higher levels want to look at.
  65.    These are all extern'd in termchar.h */
  66.  
  67. int must_write_spaces;        /* Nonzero means spaces in the text
  68.                    must actually be output; can't just skip
  69.                    over some columns to leave them blank.  */
  70. int min_padding_speed;        /* Speed below which no padding necessary */
  71.  
  72. int line_ins_del_ok;        /* Terminal can insert and delete lines */
  73. int char_ins_del_ok;        /* Terminal can insert and delete chars */
  74. int scroll_region_ok;        /* Terminal supports setting the
  75.                    scroll window */
  76. int memory_below_frame;        /* Terminal remembers lines
  77.                    scrolled off bottom */
  78. int fast_clear_end_of_line;    /* Terminal has a `ce' string */
  79.  
  80. int dont_calculate_costs;    /* Nonzero means don't bother computing */
  81.                 /* various cost tables; we won't use them.  */
  82.  
  83. /* Nonzero means no need to redraw the entire frame on resuming
  84.    a suspended Emacs.  This is useful on terminals with multiple pages,
  85.    where one page is used for Emacs and another for all else. */
  86. int no_redraw_on_reenter;
  87.  
  88. /* Hook functions that you can set to snap out the functions in this file.
  89.    These are all extern'd in termhooks.h  */
  90.  
  91. _VOID_ (*cursor_to_hook) _P_((int row, int col));
  92. _VOID_ (*raw_cursor_to_hook) _P_((int row, int col));
  93.  
  94. _VOID_ (*clear_to_end_hook) _P_((void));
  95. _VOID_ (*clear_frame_hook) _P_((void));
  96. _VOID_ (*clear_end_of_line_hook) _P_((int first_unused_hpos));
  97.  
  98. _VOID_ (*ins_del_lines_hook) _P_((int vpos, int n));
  99.  
  100. _VOID_ (*change_line_highlight_hook) _P_((int new_highlight, int vpos,
  101.                                           int first_unused_hpos));
  102. _VOID_ (*reassert_line_highlight_hook) _P_((int highlight, int vpos));
  103.  
  104. _VOID_ (*insert_glyphs_hook) _P_((register GLYPH *glyphs,
  105.                                   register int len));
  106. _VOID_ (*write_glyphs_hook) _P_((register GLYPH *glyphs,
  107.                                  register int len));
  108. _VOID_ (*delete_glyphs_hook) _P_((register int n));
  109.  
  110. _VOID_ (*ring_bell_hook) _P_((void));
  111.  
  112. _VOID_ (*reset_terminal_modes_hook) _P_((void));
  113. _VOID_ (*set_terminal_modes_hook) _P_((void));
  114. _VOID_ (*update_begin_hook) _P_((FRAME_PTR f));
  115. _VOID_ (*update_end_hook) _P_((FRAME_PTR f));
  116. _VOID_ (*set_terminal_window_hook) _P_((int size));
  117.                                       
  118. int (*read_socket_hook) _P_((int sd, struct input_event *bufp,
  119.                              int numchars, int waitp, int expected));
  120.  
  121. /* Return the current position of the mouse.
  122.  
  123.    Set *f to the frame the mouse is in, or zero if the mouse is in no
  124.    Emacs frame.  If it is set to zero, all the other arguments are
  125.    garbage.
  126.  
  127.    If the motion started in a scroll bar, set *bar_window to the
  128.    scroll bar's window, *part to the part the mouse is currently over,
  129.    *x to the position of the mouse along the scroll bar, and *y to the
  130.    overall length of the scroll bar.
  131.  
  132.    Otherwise, set *bar_window to Qnil, and *x and *y to the column and
  133.    row of the character cell the mouse is over.
  134.  
  135.    Set *time to the time the mouse was at the returned position.
  136.  
  137.    This should clear mouse_moved until the next motion
  138.    event arrives.  */
  139. void (*mouse_position_hook) _P_((FRAME_PTR *f,
  140.                                  Lisp_Object *bar_window,
  141.                                  enum scroll_bar_part *part,
  142.                                  Lisp_Object *x,
  143.                                  Lisp_Object *y,
  144.                                  unsigned long *time));
  145.  
  146. /* When reading from a minibuffer in a different frame, Emacs wants
  147.    to shift the highlight from the selected frame to the minibuffer's
  148.    frame; under X, this means it lies about where the focus is.
  149.    This hook tells the window system code to re-decide where to put
  150.    the highlight.  */
  151. void (*frame_rehighlight_hook) _P_((void));
  152.  
  153. /* If we're displaying frames using a window system that can stack
  154.    frames on top of each other, this hook allows you to bring a frame
  155.    to the front, or bury it behind all the other windows.  If this
  156.    hook is zero, that means the device we're displaying on doesn't
  157.    support overlapping frames, so there's no need to raise or lower
  158.    anything.
  159.  
  160.    If RAISE is non-zero, F is brought to the front, before all other
  161.    windows.  If RAISE is zero, F is sent to the back, behind all other
  162.    windows.  */
  163. void (*frame_raise_lower_hook) _P_((FRAME_PTR f, int raise));
  164.  
  165. /* Set the vertical scroll bar for WINDOW to have its upper left corner
  166.    at (TOP, LEFT), and be LENGTH rows high.  Set its handle to
  167.    indicate that we are displaying PORTION characters out of a total
  168.    of WHOLE characters, starting at POSITION.  If WINDOW doesn't yet
  169.    have a scroll bar, create one for it.  */
  170. void (*set_vertical_scroll_bar_hook) _P_((struct window *window, int portion,
  171.                                           int whole, int position));
  172.  
  173.  
  174. /* The following three hooks are used when we're doing a thorough
  175.    redisplay of the frame.  We don't explicitly know which scroll bars
  176.    are going to be deleted, because keeping track of when windows go
  177.    away is a real pain - can you say set-window-configuration?
  178.    Instead, we just assert at the beginning of redisplay that *all*
  179.    scroll bars are to be removed, and then save scroll bars from the
  180.    firey pit when we actually redisplay their window.  */
  181.  
  182. /* Arrange for all scroll bars on FRAME to be removed at the next call
  183.    to `*judge_scroll_bars_hook'.  A scroll bar may be spared if
  184.    `*redeem_scroll_bar_hook' is applied to its window before the judgement. 
  185.  
  186.    This should be applied to each frame each time its window tree is
  187.    redisplayed, even if it is not displaying scroll bars at the moment;
  188.    if the HAS_SCROLL_BARS flag has just been turned off, only calling
  189.    this and the judge_scroll_bars_hook will get rid of them.
  190.  
  191.    If non-zero, this hook should be safe to apply to any frame,
  192.    whether or not it can support scroll bars, and whether or not it is
  193.    currently displaying them.  */
  194. void (*condemn_scroll_bars_hook) _P_((FRAME_PTR frame));
  195.  
  196. /* Unmark WINDOW's scroll bar for deletion in this judgement cycle.
  197.    Note that it's okay to redeem a scroll bar that is not condemned.  */
  198. void (*redeem_scroll_bar_hook) _P_((struct window *window));
  199.  
  200. /* Remove all scroll bars on FRAME that haven't been saved since the
  201.    last call to `*condemn_scroll_bars_hook'.  
  202.  
  203.    This should be applied to each frame after each time its window
  204.    tree is redisplayed, even if it is not displaying scroll bars at the
  205.    moment; if the HAS_SCROLL_BARS flag has just been turned off, only
  206.    calling this and condemn_scroll_bars_hook will get rid of them.
  207.  
  208.    If non-zero, this hook should be safe to apply to any frame,
  209.    whether or not it can support scroll bars, and whether or not it is
  210.    currently displaying them.  */
  211. void (*judge_scroll_bars_hook) _P_((FRAME_PTR frame));
  212.  
  213.  
  214. /* Strings, numbers and flags taken from the termcap entry.  */
  215.  
  216. char *TS_ins_line;        /* termcap "al" */
  217. char *TS_ins_multi_lines;    /* "AL" (one parameter, # lines to insert) */
  218. char *TS_bell;            /* "bl" */
  219. char *TS_clr_to_bottom;        /* "cd" */
  220. char *TS_clr_line;        /* "ce", clear to end of line */
  221. char *TS_clr_frame;        /* "cl" */
  222. char *TS_set_scroll_region;    /* "cs" (2 params, first line and last line) */
  223. char *TS_set_scroll_region_1;   /* "cS" (4 params: total lines,
  224.                    lines above scroll region, lines below it,
  225.                    total lines again) */
  226. char *TS_del_char;        /* "dc" */
  227. char *TS_del_multi_chars;    /* "DC" (one parameter, # chars to delete) */
  228. char *TS_del_line;        /* "dl" */
  229. char *TS_del_multi_lines;    /* "DL" (one parameter, # lines to delete) */
  230. char *TS_delete_mode;        /* "dm", enter character-delete mode */
  231. char *TS_end_delete_mode;    /* "ed", leave character-delete mode */
  232. char *TS_end_insert_mode;    /* "ei", leave character-insert mode */
  233. char *TS_ins_char;        /* "ic" */
  234. char *TS_ins_multi_chars;    /* "IC" (one parameter, # chars to insert) */
  235. char *TS_insert_mode;        /* "im", enter character-insert mode */
  236. char *TS_pad_inserted_char;    /* "ip".  Just padding, no commands.  */
  237. char *TS_end_keypad_mode;    /* "ke" */
  238. char *TS_keypad_mode;        /* "ks" */
  239. char *TS_pad_char;        /* "pc", char to use as padding */
  240. char *TS_repeat;        /* "rp" (2 params, # times to repeat
  241.                    and character to be repeated) */
  242. char *TS_end_standout_mode;    /* "se" */
  243. char *TS_fwd_scroll;        /* "sf" */
  244. char *TS_standout_mode;        /* "so" */
  245. char *TS_rev_scroll;        /* "sr" */
  246. char *TS_end_termcap_modes;    /* "te" */
  247. char *TS_termcap_modes;        /* "ti" */
  248. char *TS_visible_bell;        /* "vb" */
  249. char *TS_end_visual_mode;    /* "ve" */
  250. char *TS_visual_mode;        /* "vi" */
  251. char *TS_set_window;        /* "wi" (4 params, start and end of window,
  252.                    each as vpos and hpos) */
  253.  
  254. int TF_hazeltine;    /* termcap hz flag. */
  255. int TF_insmode_motion;    /* termcap mi flag: can move while in insert mode. */
  256. int TF_standout_motion;    /* termcap mi flag: can move while in standout mode. */
  257. int TF_underscore;    /* termcap ul flag: _ underlines if overstruck on
  258.                nonblank position.  Must clear before writing _.  */
  259. int TF_teleray;        /* termcap xt flag: many weird consequences.
  260.                For t1061. */
  261.  
  262. int TF_xs;        /* Nonzero for "xs".  If set together with
  263.                TN_standout_width == 0, it means don't bother
  264.                to write any end-standout cookies.  */
  265.  
  266. int TN_standout_width;    /* termcap sg number: width occupied by standout
  267.                markers */
  268.  
  269. static int RPov;    /* # chars to start a TS_repeat */
  270.  
  271. static int delete_in_insert_mode;    /* delete mode == insert mode */
  272.  
  273. static int se_is_so;    /* 1 if same string both enters and leaves
  274.                standout mode */
  275.  
  276. /* internal state */
  277.  
  278. /* Number of chars of space used for standout marker at beginning of line,
  279.    or'd with 0100.  Zero if no standout marker at all.
  280.  
  281.    Used IFF TN_standout_width >= 0. */
  282.  
  283. static char *chars_wasted;
  284. static char *copybuf;
  285.  
  286. /* nonzero means supposed to write text in standout mode.  */
  287. int standout_requested;
  288.  
  289. int insert_mode;            /* Nonzero when in insert mode.  */
  290. int standout_mode;            /* Nonzero when in standout mode.  */
  291.  
  292. /* Size of window specified by higher levels.
  293.    This is the number of lines, from the top of frame downwards,
  294.    which can participate in insert-line/delete-line operations.
  295.  
  296.    Effectively it excludes the bottom frame_height - specified_window_size
  297.    lines from those operations.  */
  298.  
  299. int specified_window;
  300.  
  301. /* Frame currently being redisplayed; 0 if not currently redisplaying.
  302.    (Direct output does not count).  */
  303.  
  304. FRAME_PTR updating_frame;
  305.  
  306.  
  307. _VOID_
  308. ring_bell ()
  309. {
  310. #ifndef WINDOWSNT
  311.   if (! FRAME_TERMCAP_P (selected_frame))
  312. #endif /* !WINDOWSNT */
  313.     {
  314.       (*ring_bell_hook) ();
  315.       return;
  316.     }
  317.   OUTPUT (TS_visible_bell && visible_bell ? TS_visible_bell : TS_bell);
  318. }
  319.  
  320. _VOID_
  321. set_terminal_modes ()
  322. {
  323. #ifndef WINDOWSNT
  324.   if (! FRAME_TERMCAP_P (selected_frame))
  325. #endif /* !WINDOWSNT */
  326.     {
  327.       (*set_terminal_modes_hook) ();
  328.       return;
  329.     }
  330.   OUTPUT_IF (TS_termcap_modes);
  331.   OUTPUT_IF (TS_visual_mode);
  332.   OUTPUT_IF (TS_keypad_mode);
  333.   losecursor ();
  334. }
  335.  
  336. _VOID_
  337. reset_terminal_modes ()
  338. {
  339. #ifndef WINDOWSNT
  340.   if (! FRAME_TERMCAP_P (selected_frame))
  341. #endif /* !WINDOWSNT */
  342.     {
  343.       (*reset_terminal_modes_hook) ();
  344.       return;
  345.     }
  346.   if (TN_standout_width < 0)
  347.     turn_off_highlight ();
  348.   turn_off_insert ();
  349.   OUTPUT_IF (TS_end_keypad_mode);
  350.   OUTPUT_IF (TS_end_visual_mode);
  351.   OUTPUT_IF (TS_end_termcap_modes);
  352.   /* Output raw CR so kernel can track the cursor hpos.  */
  353.   /* But on magic-cookie terminals this can erase an end-standout marker and
  354.      cause the rest of the frame to be in standout, so move down first.  */
  355.   if (TN_standout_width >= 0)
  356.     cmputc ('\n');
  357.   cmputc ('\r');
  358. }
  359.  
  360. _VOID_
  361. update_begin (f)
  362.      FRAME_PTR f;
  363. {
  364.   updating_frame = f;
  365. #ifndef WINDOWSNT
  366.   if (! FRAME_TERMCAP_P (updating_frame))
  367. #endif /* !WINDOWSNT */
  368.     (*update_begin_hook) (f);
  369. }
  370.  
  371. _VOID_
  372. update_end (f)
  373.      FRAME_PTR f;
  374. {
  375. #ifndef WINDOWSNT
  376.   if (! FRAME_TERMCAP_P (updating_frame))
  377. #endif /* !WINDOWSNT */
  378.     {
  379.       (*update_end_hook) (f);
  380.       updating_frame = 0;
  381.       return;
  382.     }
  383.   turn_off_insert ();
  384.   background_highlight ();
  385.   standout_requested = 0;
  386.   updating_frame = 0;
  387. }
  388.  
  389. _VOID_
  390. set_terminal_window (size)
  391.      int size;
  392. {
  393. #ifndef WINDOWSNT
  394.   if (! FRAME_TERMCAP_P (updating_frame))
  395. #endif /* !WINDOWSNT */
  396.     {
  397.       (*set_terminal_window_hook) (size);
  398.       return;
  399.     }
  400.   specified_window = size ? size : FRAME_HEIGHT (selected_frame);
  401.   if (!scroll_region_ok)
  402.     return;
  403.   set_scroll_region (0, specified_window);
  404. }
  405.  
  406. _VOID_
  407. set_scroll_region (start, stop)
  408.      int start, stop;
  409. {
  410.   char *buf;
  411.   if (TS_set_scroll_region)
  412.     {
  413.       buf = tparam (TS_set_scroll_region, 0, 0, start, stop - 1);
  414.     }
  415.   else if (TS_set_scroll_region_1)
  416.     {
  417.       buf = tparam (TS_set_scroll_region_1, 0, 0,
  418.             FRAME_HEIGHT (selected_frame), start,
  419.             FRAME_HEIGHT (selected_frame) - stop,
  420.             FRAME_HEIGHT (selected_frame));
  421.     }
  422.   else
  423.     {
  424.       buf = tparam (TS_set_window, 0, 0, start, 0, stop, FRAME_WIDTH (selected_frame));
  425.     }
  426.   OUTPUT (buf);
  427.   xfree (buf);
  428.   losecursor ();
  429. }
  430.  
  431. _VOID_
  432. turn_on_insert ()
  433. {
  434.   if (!insert_mode)
  435.     OUTPUT (TS_insert_mode);
  436.   insert_mode = 1;
  437. }
  438.  
  439. _VOID_
  440. turn_off_insert ()
  441. {
  442.   if (insert_mode)
  443.     OUTPUT (TS_end_insert_mode);
  444.   insert_mode = 0;
  445. }
  446.  
  447. /* Handle highlighting when TN_standout_width (termcap sg) is not specified.
  448.    In these terminals, output is affected by the value of standout
  449.    mode when the output is written.
  450.  
  451.    These functions are called on all terminals, but do nothing
  452.    on terminals whose standout mode does not work that way.  */
  453.  
  454. _VOID_
  455. turn_off_highlight ()
  456. {
  457.   if (TN_standout_width < 0)
  458.     {
  459.       if (standout_mode)
  460.     OUTPUT_IF (TS_end_standout_mode);
  461.       standout_mode = 0;
  462.     }
  463. }
  464.  
  465. _VOID_
  466. turn_on_highlight ()
  467. {
  468.   if (TN_standout_width < 0)
  469.     {
  470.       if (!standout_mode)
  471.     OUTPUT_IF (TS_standout_mode);
  472.       standout_mode = 1;
  473.     }
  474. }
  475.  
  476. /* Set standout mode to the state it should be in for
  477.    empty space inside windows.  What this is,
  478.    depends on the user option inverse-video.  */
  479.  
  480. _VOID_
  481. background_highlight ()
  482. {
  483.   if (TN_standout_width >= 0)
  484.     return;
  485.   if (inverse_video)
  486.     turn_on_highlight ();
  487.   else
  488.     turn_off_highlight ();
  489. }
  490.  
  491. /* Set standout mode to the mode specified for the text to be output.  */
  492.  
  493. static _VOID_
  494. highlight_if_desired ()
  495. {
  496.   if (TN_standout_width >= 0)
  497.     return;
  498.   if (!inverse_video == !standout_requested)
  499.     turn_off_highlight ();
  500.   else
  501.     turn_on_highlight ();
  502. }
  503.  
  504. /* Handle standout mode for terminals in which TN_standout_width >= 0.
  505.    On these terminals, standout is controlled by markers that
  506.    live inside the terminal's memory.  TN_standout_width is the width
  507.    that the marker occupies in memory.  Standout runs from the marker
  508.    to the end of the line on some terminals, or to the next
  509.    turn-off-standout marker (TS_end_standout_mode) string
  510.    on other terminals.  */
  511.  
  512. /* Write a standout marker or end-standout marker at the front of the line
  513.    at vertical position vpos.  */
  514.  
  515. _VOID_
  516. write_standout_marker (flag, vpos)
  517.      int flag, vpos;
  518. {
  519.   if (flag || (TS_end_standout_mode && !TF_teleray && !se_is_so
  520.            && !(TF_xs && TN_standout_width == 0)))
  521.     {
  522.       cmgoto (vpos, 0);
  523.       cmplus (TN_standout_width);
  524.       OUTPUT (flag ? TS_standout_mode : TS_end_standout_mode);
  525.       chars_wasted[curY] = TN_standout_width | 0100;
  526.     }
  527. }
  528.  
  529. /* External interface to control of standout mode.
  530.    Call this when about to modify line at position VPOS
  531.    and not change whether it is highlighted.  */
  532.  
  533. _VOID_
  534. reassert_line_highlight (highlight, vpos)
  535.      int highlight;
  536.      int vpos;
  537. {
  538. #ifndef WINDOWSNT
  539.   if (! FRAME_TERMCAP_P ((updating_frame ? updating_frame : selected_frame)))
  540. #endif /* !WINDOWSNT */
  541.     {
  542.       (*reassert_line_highlight_hook) (highlight, vpos);
  543.       return;
  544.     }
  545.   if (TN_standout_width < 0)
  546.     /* Handle terminals where standout takes affect at output time */
  547.     standout_requested = highlight;
  548.   else if (chars_wasted[vpos] == 0)
  549.     /* For terminals with standout markers, write one on this line
  550.        if there isn't one already.  */
  551.     write_standout_marker (highlight, vpos);
  552. }
  553.  
  554. /* Call this when about to modify line at position VPOS
  555.    and change whether it is highlighted.  */
  556.  
  557. _VOID_
  558. change_line_highlight (new_highlight, vpos, first_unused_hpos)
  559.      int new_highlight, vpos, first_unused_hpos;
  560. {
  561.   standout_requested = new_highlight;
  562. #ifndef WINDOWSNT
  563.   if (! FRAME_TERMCAP_P (updating_frame))
  564. #endif /* !WINDOWSNT */
  565.     {
  566.       (*change_line_highlight_hook) (new_highlight, vpos, first_unused_hpos);
  567.       return;
  568.     }
  569.  
  570.   cursor_to (vpos, 0);
  571.  
  572.   if (TN_standout_width < 0)
  573.     background_highlight ();
  574.   /* If line starts with a marker, delete the marker */
  575.   else if (TS_clr_line && chars_wasted[curY])
  576.     {
  577.       turn_off_insert ();
  578.       /* On Teleray, make sure to erase the SO marker.  */
  579.       if (TF_teleray)
  580.     {
  581.       cmgoto (curY - 1, FRAME_WIDTH (selected_frame) - 4);
  582.       OUTPUT ("\033S");
  583.       curY++;        /* ESC S moves to next line where the TS_standout_mode was */
  584.       curX = 0;
  585.     }
  586.       else
  587.     cmgoto (curY, 0);    /* reposition to kill standout marker */
  588.     }
  589.   clear_end_of_line_raw (first_unused_hpos);
  590.   reassert_line_highlight (new_highlight, curY);
  591. }
  592.  
  593.  
  594. /* Move to absolute position, specified origin 0 */
  595.  
  596. _VOID_
  597. cursor_to (row, col)
  598.      int row, col;
  599. {
  600.   if (
  601. #ifndef WINDOWSNT
  602.       ! FRAME_TERMCAP_P ((updating_frame
  603.                 ? updating_frame
  604.                 : selected_frame))
  605.       && 
  606. #endif /* !WINDOWSNT */
  607.       cursor_to_hook)
  608.     {
  609.       (*cursor_to_hook) (row, col);
  610.       return;
  611.     }
  612.  
  613.   col += chars_wasted[row] & 077;
  614.   if (curY == row && curX == col)
  615.     return;
  616.   if (!TF_standout_motion)
  617.     background_highlight ();
  618.   if (!TF_insmode_motion)
  619.     turn_off_insert ();
  620.   cmgoto (row, col);
  621. }
  622.  
  623. /* Similar but don't take any account of the wasted characters.  */
  624.  
  625. _VOID_
  626. raw_cursor_to (row, col)
  627.      int row, col;
  628. {
  629. #ifndef WINDOWSNT
  630.   if (! FRAME_TERMCAP_P ((updating_frame ? updating_frame : selected_frame)))
  631. #endif /* !WINDOWSNT */
  632.     {
  633.       (*raw_cursor_to_hook) (row, col);
  634.       return;
  635.     }
  636.   if (curY == row && curX == col)
  637.     return;
  638.   if (!TF_standout_motion)
  639.     background_highlight ();
  640.   if (!TF_insmode_motion)
  641.     turn_off_insert ();
  642.   cmgoto (row, col);
  643. }
  644.  
  645. /* Erase operations */
  646.  
  647. /* clear from cursor to end of frame */
  648. _VOID_
  649. clear_to_end ()
  650. {
  651.   register int i;
  652.  
  653.   if (clear_to_end_hook 
  654. #ifndef WINDOWSNT
  655.       && FRAME_TERMCAP_P (updating_frame)
  656. #endif /* !WINDOWSNT */
  657.       )
  658.     {
  659.       (*clear_to_end_hook) ();
  660.       return;
  661.     }
  662.   if (TS_clr_to_bottom)
  663.     {
  664.       background_highlight ();
  665.       OUTPUT (TS_clr_to_bottom);
  666.       bzero (chars_wasted + curY, FRAME_HEIGHT (selected_frame) - curY);
  667.     }
  668.   else
  669.     {
  670.       for (i = curY; i < FRAME_HEIGHT (selected_frame); i++)
  671.     {
  672.       cursor_to (i, 0);
  673.       clear_end_of_line_raw (FRAME_WIDTH (selected_frame));
  674.     }
  675.     }
  676. }
  677.  
  678. /* Clear entire frame */
  679.  
  680. _VOID_
  681. clear_frame ()
  682. {
  683.   if (clear_frame_hook
  684. #ifndef WINDOWSNT
  685.       && ! FRAME_TERMCAP_P ((updating_frame ? updating_frame : selected_frame))
  686. #endif /* !WINDOWSNT */
  687.       )
  688.     {
  689.       (*clear_frame_hook) ();
  690.       return;
  691.     }
  692.   if (TS_clr_frame)
  693.     {
  694.       background_highlight ();
  695.       OUTPUT (TS_clr_frame);
  696.       bzero (chars_wasted, FRAME_HEIGHT (selected_frame));
  697.       cmat (0, 0);
  698.     }
  699.   else
  700.     {
  701.       cursor_to (0, 0);
  702.       clear_to_end ();
  703.     }
  704. }
  705.  
  706. /* Clear to end of line, but do not clear any standout marker.
  707.    Assumes that the cursor is positioned at a character of real text,
  708.    which implies it cannot be before a standout marker
  709.    unless the marker has zero width.
  710.  
  711.    Note that the cursor may be moved.  */
  712.  
  713. _VOID_
  714. clear_end_of_line (first_unused_hpos)
  715.      int first_unused_hpos;
  716. {
  717.   static GLYPH buf = SPACEGLYPH;
  718.   if (FRAME_TERMCAP_P (selected_frame)
  719.       && TN_standout_width == 0 && curX == 0 && chars_wasted[curY] != 0)
  720.     write_glyphs (&buf, 1);
  721.   clear_end_of_line_raw (first_unused_hpos);
  722. }
  723.  
  724. /* Clear from cursor to end of line.
  725.    Assume that the line is already clear starting at column first_unused_hpos.
  726.    If the cursor is at a standout marker, erase the marker.
  727.  
  728.    Note that the cursor may be moved, on terminals lacking a `ce' string.  */
  729.  
  730. _VOID_
  731. clear_end_of_line_raw (first_unused_hpos)
  732.      int first_unused_hpos;
  733. {
  734.   register int i;
  735.  
  736.   if (clear_end_of_line_hook
  737. #ifndef WINDOWSNT      
  738.       && ! FRAME_TERMCAP_P ((updating_frame
  739.                    ? updating_frame
  740.                    : selected_frame))
  741. #endif /* !WINDOWSNT */
  742.       )
  743.     {
  744.       (*clear_end_of_line_hook) (first_unused_hpos);
  745.       return;
  746.     }
  747.  
  748.   first_unused_hpos += chars_wasted[curY] & 077;
  749.   if (curX >= first_unused_hpos)
  750.     return;
  751.   /* Notice if we are erasing a magic cookie */
  752.   if (curX == 0)
  753.     chars_wasted[curY] = 0;
  754.   background_highlight ();
  755.   if (TS_clr_line)
  756.     {
  757.       OUTPUT1 (TS_clr_line);
  758.     }
  759.   else
  760.     {            /* have to do it the hard way */
  761.       turn_off_insert ();
  762.  
  763.       /* Do not write in last row last col with Autowrap on. */
  764.       if (AutoWrap && curY == FRAME_HEIGHT (selected_frame) - 1
  765.       && first_unused_hpos == FRAME_WIDTH (selected_frame))
  766.     first_unused_hpos--;
  767.  
  768.       for (i = curX; i < first_unused_hpos; i++)
  769.     {
  770.       if (termscript)
  771.         fputc (' ', termscript);
  772.       putchar (' ');
  773.     }
  774.       cmplus (first_unused_hpos - curX);
  775.     }
  776. }
  777.  
  778.  
  779. _VOID_
  780. write_glyphs (string, len)
  781.      register GLYPH *string;
  782.      register int len;
  783. {
  784.   register GLYPH g;
  785.   register int tlen = GLYPH_TABLE_LENGTH;
  786.   register Lisp_Object *tbase = GLYPH_TABLE_BASE;
  787.  
  788.   if (write_glyphs_hook
  789. #ifndef WINDOWSNT
  790.       && ! FRAME_TERMCAP_P ((updating_frame ? updating_frame : selected_frame))
  791. #endif /* !WINDOWSNY */
  792.       )
  793.     {
  794.       (*write_glyphs_hook) (string, len);
  795.       return;
  796.     }
  797.  
  798.   highlight_if_desired ();
  799.   turn_off_insert ();
  800.  
  801.   /* Don't dare write in last column of bottom line, if AutoWrap,
  802.      since that would scroll the whole frame on some terminals.  */
  803.  
  804.   if (AutoWrap
  805.       && curY + 1 == FRAME_HEIGHT (selected_frame)
  806.       && (curX + len - (chars_wasted[curY] & 077)
  807.       == FRAME_WIDTH (selected_frame)))
  808.     len --;
  809.  
  810.   cmplus (len);
  811.   while (--len >= 0)
  812.     {
  813.       g = *string++;
  814.       /* Check quickly for G beyond length of table.
  815.      That implies it isn't an alias and is simple.  */
  816.       if (g >= (unsigned)tlen)
  817.     {
  818.     simple:
  819.       putc (g & 0xff, stdout);
  820.       if (ferror (stdout))
  821.         clearerr (stdout);
  822.       if (termscript)
  823.         putc (g & 0xff, termscript);
  824.     }
  825.       else
  826.     {
  827.       /* G has an entry in Vglyph_table,
  828.          so process any alias and then test for simpleness.  */
  829.       while (GLYPH_ALIAS_P (tbase, (unsigned)tlen, g))
  830.         g = GLYPH_ALIAS (tbase, g);
  831.       if (GLYPH_SIMPLE_P (tbase, (unsigned)tlen, g))
  832.         goto simple;
  833.       else
  834.         {
  835.           /* Here if G (or its definition as an alias) is not simple.  */
  836.           fwrite (GLYPH_STRING (tbase, g), 1, GLYPH_LENGTH (tbase, g),
  837.               stdout);
  838.           if (ferror (stdout))
  839.         clearerr (stdout);
  840.           if (termscript)
  841.         fwrite (GLYPH_STRING (tbase, g), 1, GLYPH_LENGTH (tbase, g),
  842.             termscript);
  843.         }
  844.     }
  845.     }
  846. }
  847.  
  848. /* If start is zero, insert blanks instead of a string at start */
  849.  
  850. _VOID_
  851. insert_glyphs (start, len)
  852.      register GLYPH *start;
  853.      register int len;
  854. {
  855.   char *buf;
  856.   register GLYPH g;
  857.   register int tlen = GLYPH_TABLE_LENGTH;
  858.   register Lisp_Object *tbase = GLYPH_TABLE_BASE;
  859.  
  860.   if (insert_glyphs_hook 
  861. #ifndef WINDOWSNT
  862.       && ! FRAME_TERMCAP_P (updating_frame)
  863. #endif /* !WINDOWSNT */
  864.       )
  865.     {
  866.       (*insert_glyphs_hook) (start, len);
  867.       return;
  868.     }
  869.  
  870.   highlight_if_desired ();
  871.  
  872.   if (TS_ins_multi_chars)
  873.     {
  874.       buf = tparam (TS_ins_multi_chars, 0, 0, len);
  875.       OUTPUT1 (buf);
  876.       xfree (buf);
  877.       if (start)
  878.     write_glyphs (start, len);
  879.       return;
  880.     }
  881.  
  882.   turn_on_insert ();
  883.   cmplus (len);
  884.   while (--len >= 0)
  885.     {
  886.       OUTPUT1_IF (TS_ins_char);
  887.       if (!start)
  888.     g = SPACEGLYPH;
  889.       else
  890.     g = *start++;
  891.  
  892.       if (GLYPH_SIMPLE_P (tbase, (unsigned)tlen, g))
  893.     {
  894.       putc (g & 0xff, stdout);
  895.       if (ferror (stdout))
  896.         clearerr (stdout);
  897.       if (termscript)
  898.         putc (g & 0xff, termscript);
  899.     }
  900.       else
  901.     {
  902.       fwrite (GLYPH_STRING (tbase, g), 1, GLYPH_LENGTH (tbase, g), stdout);
  903.       if (ferror (stdout))
  904.         clearerr (stdout);
  905.       if (termscript)
  906.         fwrite (GLYPH_STRING (tbase, g), 1, GLYPH_LENGTH (tbase, g),
  907.             termscript);
  908.     }
  909.  
  910.     OUTPUT1_IF (TS_pad_inserted_char);
  911.       }
  912. }
  913.  
  914. _VOID_
  915. delete_glyphs (n)
  916.      register int n;
  917. {
  918.   char *buf;
  919.   register int i;
  920.  
  921.   if (delete_glyphs_hook 
  922. #ifndef WINDOWSNT
  923.       && ! FRAME_TERMCAP_P (updating_frame)
  924. #endif /* !WINDOWSNT */
  925.       )
  926.     {
  927.       (*delete_glyphs_hook) (n);
  928.       return;
  929.     }
  930.  
  931.   if (delete_in_insert_mode)
  932.     {
  933.       turn_on_insert ();
  934.     }
  935.   else
  936.     {
  937.       turn_off_insert ();
  938.       OUTPUT_IF (TS_delete_mode);
  939.     }
  940.  
  941.   if (TS_del_multi_chars)
  942.     {
  943.       buf = tparam (TS_del_multi_chars, 0, 0, n);
  944.       OUTPUT1 (buf);
  945.       xfree (buf);
  946.     }
  947.   else
  948.     for (i = 0; i < n; i++)
  949.       OUTPUT1 (TS_del_char);
  950.   if (!delete_in_insert_mode)
  951.     OUTPUT_IF (TS_end_delete_mode);
  952. }
  953.  
  954. /* Insert N lines at vpos VPOS.  If N is negative, delete -N lines.  */
  955.  
  956. _VOID_
  957. ins_del_lines (vpos, n)
  958.      int vpos, n;
  959. {
  960.   char *multi = n > 0 ? TS_ins_multi_lines : TS_del_multi_lines;
  961.   char *single = n > 0 ? TS_ins_line : TS_del_line;
  962.   char *scroll = n > 0 ? TS_rev_scroll : TS_fwd_scroll;
  963.  
  964.   register int i = n > 0 ? n : -n;
  965.   register char *buf;
  966.  
  967. #ifndef WINDOWSNT
  968.   if (ins_del_lines_hook && ! FRAME_TERMCAP_P (updating_frame)
  969.       )
  970.     {
  971.       (*ins_del_lines_hook) (vpos, n);
  972.       return;
  973.     }
  974. #endif /* !WINDOWSNT */
  975.  
  976.   /* If the lines below the insertion are being pushed
  977.      into the end of the window, this is the same as clearing;
  978.      and we know the lines are already clear, since the matching
  979.      deletion has already been done.  So can ignore this.  */
  980.   /* If the lines below the deletion are blank lines coming
  981.      out of the end of the window, don't bother,
  982.      as there will be a matching inslines later that will flush them. */
  983.   if (scroll_region_ok && vpos + i >= specified_window)
  984.     return;
  985.   if (!memory_below_frame && vpos + i >= FRAME_HEIGHT (selected_frame))
  986.     return;
  987.  
  988. #ifdef WINDOWSNT
  989.   // Do the above checks before calling the hook, so the hook
  990.   // doesn't have to worry about anomalous cases.
  991.   if (ins_del_lines_hook)
  992.     {
  993.       (*ins_del_lines_hook) (vpos, n);
  994.       return;
  995.     }
  996. #endif /* WINDOWSNT */
  997.  
  998.   if (multi)
  999.     {
  1000.       raw_cursor_to (vpos, 0);
  1001.       background_highlight ();
  1002.       buf = tparam (multi, 0, 0, i);
  1003.       OUTPUT (buf);
  1004.       xfree (buf);
  1005.     }
  1006.   else if (single)
  1007.     {
  1008.       raw_cursor_to (vpos, 0);
  1009.       background_highlight ();
  1010.       while (--i >= 0)
  1011.     OUTPUT (single);
  1012.       if (TF_teleray)
  1013.     curX = 0;
  1014.     }
  1015.   else
  1016.     {
  1017.       set_scroll_region (vpos, specified_window);
  1018.       if (n < 0)
  1019.     raw_cursor_to (specified_window - 1, 0);
  1020.       else
  1021.     raw_cursor_to (vpos, 0);
  1022.       background_highlight ();
  1023.       while (--i >= 0)
  1024.     OUTPUTL (scroll, specified_window - vpos);
  1025.       set_scroll_region (0, specified_window);
  1026.     }
  1027.  
  1028.   if (TN_standout_width >= 0)
  1029.     {
  1030.       register lower_limit
  1031.     = (scroll_region_ok
  1032.        ? specified_window
  1033.        : FRAME_HEIGHT (selected_frame));
  1034.  
  1035.       if (n < 0)
  1036.     {
  1037.       bcopy (&chars_wasted[vpos - n], &chars_wasted[vpos],
  1038.          lower_limit - vpos + n);
  1039.       bzero (&chars_wasted[lower_limit + n], - n);
  1040.     }
  1041.       else
  1042.     {
  1043.       bcopy (&chars_wasted[vpos], ©buf[vpos], lower_limit - vpos - n);
  1044.       bcopy (©buf[vpos], &chars_wasted[vpos + n],
  1045.          lower_limit - vpos - n);
  1046.       bzero (&chars_wasted[vpos], n);
  1047.     }
  1048.     }
  1049.   if (!scroll_region_ok && memory_below_frame && n < 0)
  1050.     {
  1051.       cursor_to (FRAME_HEIGHT (selected_frame) + n, 0);
  1052.       clear_to_end ();
  1053.     }
  1054. }
  1055.  
  1056. /* Compute cost of sending "str", in characters,
  1057.    not counting any line-dependent padding.  */
  1058.  
  1059. /* From cm.c */
  1060. extern int cost;
  1061.  
  1062. int
  1063. string_cost (str)
  1064.      char *str;
  1065. {
  1066.   cost = 0;
  1067.   if (str)
  1068.     tputs (str, 0, evalcost);
  1069.   return cost;
  1070. }
  1071.  
  1072. /* Compute cost of sending "str", in characters,
  1073.    counting any line-dependent padding at one line.  */
  1074.  
  1075. static int
  1076. string_cost_one_line (str)
  1077.      char *str;
  1078. {
  1079.   cost = 0;
  1080.   if (str)
  1081.     tputs (str, 1, evalcost);
  1082.   return cost;
  1083. }
  1084.  
  1085. /* Compute per line amount of line-dependent padding,
  1086.    in tenths of characters.  */
  1087.  
  1088. int
  1089. per_line_cost (str)
  1090.      register char *str;
  1091. {
  1092.   cost = 0;
  1093.   if (str)
  1094.     tputs (str, 0, evalcost);
  1095.   cost = - cost;
  1096.   if (str)
  1097.     tputs (str, 10, evalcost);
  1098.   return cost;
  1099. }
  1100.  
  1101. #ifndef old
  1102. /* char_ins_del_cost[n] is cost of inserting N characters.
  1103.    char_ins_del_cost[-n] is cost of deleting N characters. */
  1104.  
  1105. int *char_ins_del_vector;
  1106.  
  1107. #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_WIDTH ((f))])
  1108. #endif
  1109.  
  1110. /* ARGSUSED */
  1111. static void
  1112. calculate_ins_del_char_costs (frame)
  1113.      FRAME_PTR frame;
  1114. {
  1115.   int ins_startup_cost, del_startup_cost;
  1116.   int ins_cost_per_char, del_cost_per_char;
  1117.   register int i;
  1118.   register int *p;
  1119.  
  1120.   if (TS_ins_multi_chars)
  1121.     {
  1122.       ins_cost_per_char = 0;
  1123.       ins_startup_cost = string_cost_one_line (TS_ins_multi_chars);
  1124.     }
  1125.   else if (TS_ins_char || TS_pad_inserted_char
  1126.        || (TS_insert_mode && TS_end_insert_mode))
  1127.     {
  1128.       ins_startup_cost = (30 * (string_cost (TS_insert_mode)
  1129.                 + string_cost (TS_end_insert_mode))) / 100;
  1130.       ins_cost_per_char = (string_cost_one_line (TS_ins_char)
  1131.                + string_cost_one_line (TS_pad_inserted_char));
  1132.     }
  1133.   else
  1134.     {
  1135.       ins_startup_cost = 9999;
  1136.       ins_cost_per_char = 0;
  1137.     }
  1138.  
  1139.   if (TS_del_multi_chars)
  1140.     {
  1141.       del_cost_per_char = 0;
  1142.       del_startup_cost = string_cost_one_line (TS_del_multi_chars);
  1143.     }
  1144.   else if (TS_del_char)
  1145.     {
  1146.       del_startup_cost = (string_cost (TS_delete_mode)
  1147.               + string_cost (TS_end_delete_mode));
  1148.       if (delete_in_insert_mode)
  1149.     del_startup_cost /= 2;
  1150.       del_cost_per_char = string_cost_one_line (TS_del_char);
  1151.     }
  1152.   else
  1153.     {
  1154.       del_startup_cost = 9999;
  1155.       del_cost_per_char = 0;
  1156.     }
  1157.  
  1158.   /* Delete costs are at negative offsets */
  1159.   p = &char_ins_del_cost (frame)[0];
  1160.   for (i = FRAME_WIDTH (selected_frame); --i >= 0;)
  1161.     *--p = (del_startup_cost += del_cost_per_char);
  1162.  
  1163.   /* Doing nothing is free */
  1164.   p = &char_ins_del_cost (frame)[0];
  1165.   *p++ = 0;
  1166.  
  1167.   /* Insert costs are at positive offsets */
  1168.   for (i = FRAME_WIDTH (frame); --i >= 0;)
  1169.     *p++ = (ins_startup_cost += ins_cost_per_char);
  1170. }
  1171.  
  1172. #ifdef HAVE_X_WINDOWS
  1173. extern int x_screen_planes;
  1174. #endif
  1175.  
  1176. _VOID_
  1177. calculate_costs (frame)
  1178.      FRAME_PTR frame;
  1179. {
  1180.   register char *f = TS_set_scroll_region ?
  1181.                        TS_set_scroll_region
  1182.              : TS_set_scroll_region_1;
  1183.  
  1184.   if (dont_calculate_costs)
  1185.     return;
  1186.  
  1187. #ifdef HAVE_X_WINDOWS
  1188.   if (FRAME_X_P (frame))
  1189.     {
  1190.       do_line_insertion_deletion_costs (frame, 0, ".5*", 0, ".5*",
  1191.                     0, 0, x_screen_planes);
  1192.       return;
  1193.     }
  1194. #endif
  1195.  
  1196.   /* These variables are only used for terminal stuff.  They are allocated
  1197.      once for the terminal frame of X-windows emacs, but not used afterwards.
  1198.  
  1199.      char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
  1200.      X turns off char_ins_del_ok.
  1201.  
  1202.      chars_wasted and copybuf are only used here in term.c in cases where
  1203.      the term hook isn't called. */
  1204.  
  1205.   if (chars_wasted != 0)
  1206.     chars_wasted = (char *) xrealloc (chars_wasted, FRAME_HEIGHT (frame));
  1207.   else
  1208.     chars_wasted = (char *) xmalloc (FRAME_HEIGHT (frame));
  1209.  
  1210.   if (copybuf != 0)
  1211.     copybuf = (char *) xrealloc (copybuf, FRAME_HEIGHT (frame));
  1212.   else
  1213.     copybuf = (char *) xmalloc (FRAME_HEIGHT (frame));
  1214.  
  1215.   if (char_ins_del_vector != 0)
  1216.     char_ins_del_vector
  1217.       = (int *) xrealloc (char_ins_del_vector,
  1218.               (sizeof (int)
  1219.                + 2 * FRAME_WIDTH (frame) * sizeof (int)));
  1220.   else
  1221.     char_ins_del_vector
  1222.       = (int *) xmalloc (sizeof (int)
  1223.              + 2 * FRAME_WIDTH (frame) * sizeof (int));
  1224.  
  1225.   bzero (chars_wasted, FRAME_HEIGHT (frame));
  1226.   bzero (copybuf, FRAME_HEIGHT (frame));
  1227.   bzero (char_ins_del_vector, (sizeof (int)
  1228.                    + 2 * FRAME_WIDTH (frame) * sizeof (int)));
  1229.  
  1230.   if (f && (!TS_ins_line && !TS_del_line))
  1231.     do_line_insertion_deletion_costs (frame,
  1232.                       TS_rev_scroll, TS_ins_multi_lines,
  1233.                       TS_fwd_scroll, TS_del_multi_lines,
  1234.                       f, f, 1);
  1235.   else
  1236.     do_line_insertion_deletion_costs (frame,
  1237.                       TS_ins_line, TS_ins_multi_lines,
  1238.                       TS_del_line, TS_del_multi_lines,
  1239.                       0, 0, 1);
  1240.  
  1241.   calculate_ins_del_char_costs (frame);
  1242.  
  1243.   /* Don't use TS_repeat if its padding is worse than sending the chars */
  1244.   if (TS_repeat && per_line_cost (TS_repeat) * baud_rate < 9000)
  1245.     RPov = string_cost (TS_repeat);
  1246.   else
  1247.     RPov = FRAME_WIDTH (frame) * 2;
  1248.  
  1249.   cmcostinit ();        /* set up cursor motion costs */
  1250. }
  1251.  
  1252. struct fkey_table {
  1253.   char *cap, *name;
  1254. };
  1255.  
  1256.   /* Termcap capability names that correspond directly to X keysyms.
  1257.      Some of these (marked "terminfo") aren't supplied by old-style
  1258.      (Berkeley) termcap entries.  They're listed in X keysym order;
  1259.      except we put the keypad keys first, so that if they clash with
  1260.      other keys (as on the IBM PC keyboard) they get overridden.
  1261.   */
  1262.  
  1263. static struct fkey_table keys[] = {
  1264.   "kh", "home",        /* termcap */
  1265.   "kl", "left",        /* termcap */
  1266.   "ku", "up",        /* termcap */
  1267.   "kr", "right",    /* termcap */
  1268.   "kd", "down",        /* termcap */
  1269.   "%8", "prior",    /* terminfo */
  1270.   "%5", "next",        /* terminfo */
  1271.   "@7",    "end",        /* terminfo */
  1272.   "@1", "begin",    /* terminfo */
  1273.   "*6", "select",    /* terminfo */
  1274.   "%9", "print",    /* terminfo */
  1275.   "@4", "execute",    /* terminfo --- actually the `command' key */
  1276.   /*
  1277.    * "insert" --- see below
  1278.    */
  1279.   "&8",    "undo",        /* terminfo */
  1280.   "%0",    "redo",        /* terminfo */
  1281.   "%7",    "menu",        /* terminfo --- actually the `options' key */
  1282.   "@0",    "find",        /* terminfo */
  1283.   "@2",    "cancel",    /* terminfo */
  1284.   "%1", "help",        /* terminfo */
  1285.   /*
  1286.    * "break" goes here, but can't be reliably intercepted with termcap
  1287.    */
  1288.   "&4", "reset",    /* terminfo --- actually `restart' */
  1289.   /*
  1290.    * "system" and "user" --- no termcaps
  1291.    */
  1292.   "kE", "clearline",    /* terminfo */
  1293.   "kA", "insertline",    /* terminfo */
  1294.   "kL", "deleteline",    /* terminfo */
  1295.   "kI", "insertchar",    /* terminfo */
  1296.   "kD", "deletechar",    /* terminfo */
  1297.   "kB", "backtab",    /* terminfo */
  1298.   /*
  1299.    * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
  1300.    */
  1301.   "@8", "kp-enter",    /* terminfo */
  1302.   /*
  1303.    * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
  1304.    * "kp-multiply", "kp-add", "kp-separator",
  1305.    * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
  1306.    * --- no termcaps for any of these.
  1307.    */
  1308.   "K4", "kp-1",        /* terminfo */
  1309.   /*
  1310.    * "kp-2" --- no termcap
  1311.    */
  1312.   "K5", "kp-3",        /* terminfo */
  1313.   /*
  1314.    * "kp-4" --- no termcap
  1315.    */
  1316.   "K2", "kp-5",        /* terminfo */
  1317.   /*
  1318.    * "kp-6" --- no termcap
  1319.    */
  1320.   "K1", "kp-7",        /* terminfo */
  1321.   /*
  1322.    * "kp-8" --- no termcap
  1323.    */
  1324.   "K3", "kp-9",        /* terminfo */
  1325.   /*
  1326.    * "kp-equal" --- no termcap
  1327.    */
  1328.   "k1",    "f1",
  1329.   "k2",    "f2",
  1330.   "k3",    "f3",
  1331.   "k4",    "f4",
  1332.   "k5",    "f5",
  1333.   "k6",    "f6",
  1334.   "k7",    "f7",
  1335.   "k8",    "f8",
  1336.   "k9",    "f9",
  1337.   };
  1338.  
  1339. /* Find the escape codes sent by the function keys for Vfunction_key_map.
  1340.    This function scans the termcap function key sequence entries, and 
  1341.    adds entries to Vfunction_key_map for each function key it finds.  */
  1342.  
  1343. void
  1344. term_get_fkeys (address)
  1345.      char **address;
  1346. {
  1347. #ifndef USE_PROTOTYPES
  1348.   extern char *tgetstr ();
  1349. #endif
  1350.   int i;
  1351.  
  1352.   /* This can happen if CANNOT_DUMP or with strange options.  */
  1353.   if (!initialized)
  1354.     Vfunction_key_map = Fmake_sparse_keymap (Qnil);
  1355.  
  1356.   for (i = 0; i < (sizeof (keys)/sizeof (keys[0])); i++)
  1357.     {
  1358.       char *sequence = tgetstr (keys[i].cap, address);
  1359.       if (sequence)
  1360.     Fdefine_key (Vfunction_key_map,
  1361.              build_string (sequence),
  1362.              Fmake_vector (make_number (1), intern (keys[i].name)));
  1363.     }
  1364.  
  1365.   /* The uses of the "k0" capability are inconsistent; sometimes it
  1366.      describes F10, whereas othertimes it describes F0 and "k;" describes F10.
  1367.      We will attempt to politely accommodate both systems by testing for
  1368.      "k;", and if it is present, assuming that "k0" denotes F0, otherwise F10.
  1369.      */
  1370.   {
  1371.     char *k_semi  = tgetstr ("k;", address);
  1372.     char *k0      = tgetstr ("k0", address);
  1373.     char *k0_name = "f10";
  1374.  
  1375.     if (k_semi)
  1376.       {
  1377.     Fdefine_key (Vfunction_key_map,
  1378.              build_string (k_semi),
  1379.              Fmake_vector (make_number (1), intern ("f10")));
  1380.     k0_name = "f0";
  1381.       }
  1382.  
  1383.     if (k0)
  1384.       Fdefine_key (Vfunction_key_map,
  1385.            build_string (k0),
  1386.            Fmake_vector (make_number (1), intern (k0_name)));
  1387.   }
  1388.  
  1389.   /* Set up cookies for numbered function keys above f10. */
  1390.   {
  1391.     char fcap[3], fkey[4];
  1392.  
  1393.     fcap[0] = 'F'; fcap[2] = '\0';
  1394.     for (i = 11; i < 64; i++)
  1395.       {
  1396.     if (i <= 19)
  1397.       fcap[1] = '1' + i - 11;
  1398.     else if (i <= 45)
  1399.       fcap[1] = 'A' + i - 11;
  1400.     else
  1401.       fcap[1] = 'a' + i - 11;
  1402.  
  1403.     {
  1404.       char *sequence = tgetstr (fcap, address);
  1405.       if (sequence)
  1406.         {
  1407.           (void) sprintf (fkey, "f%d", i);        
  1408.           Fdefine_key (Vfunction_key_map,
  1409.                build_string (sequence),
  1410.                Fmake_vector (make_number (1), intern (fkey)));
  1411.         }
  1412.     }
  1413.       }
  1414.    }
  1415.  
  1416.   /*
  1417.    * Various mappings to try and get a better fit.
  1418.    */
  1419.   {
  1420. #define CONDITIONAL_REASSIGN(cap1, cap2, sym)                \
  1421.       if (!tgetstr (cap1, address))                    \
  1422.     {                                \
  1423.       char *sequence = tgetstr (cap2, address);            \
  1424.       if (sequence)                            \
  1425.         Fdefine_key (Vfunction_key_map,                \
  1426.              build_string (sequence),            \
  1427.              Fmake_vector (make_number (1), intern (sym)));    \
  1428.     }
  1429.       
  1430.       /* if there's no key_next keycap, map key_npage to `next' keysym */
  1431.       CONDITIONAL_REASSIGN ("%5", "kN", "next");
  1432.       /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
  1433.       CONDITIONAL_REASSIGN ("%8", "kP", "prior");
  1434.       /* if there's no key_dc keycap, map key_ic to `insert' keysym */
  1435.       CONDITIONAL_REASSIGN ("kD", "kI", "insert");
  1436. #undef CONDITIONAL_REASSIGN
  1437.   }
  1438. }
  1439.  
  1440.  
  1441. _VOID_
  1442. term_init (terminal_type)
  1443.      char *terminal_type;
  1444. {
  1445.   char *area;
  1446.   char **address = &area;
  1447.   char buffer[2044];
  1448.   register char *p;
  1449.   int status;
  1450.  
  1451. #ifdef WINDOWSNT
  1452.   initialize_win_nt_display();
  1453.  
  1454.   Wcm_clear ();
  1455.   dont_calculate_costs = 0;
  1456.  
  1457.   area = (char *) malloc (2044);
  1458.  
  1459.   if (area == 0)
  1460.     abort ();
  1461.  
  1462. /*  AutoWrap = tgetflag ("am");                */
  1463. /*  memory_below_screen = tgetflag ("db");        */
  1464. /*  must_write_spaces = tgetflag ("in");        */
  1465. /*  meta_key = tgetflag ("km") || tgetflag ("MT");    */
  1466. /*  TF_insmode_motion = tgetflag ("mi");        */
  1467. /*  TF_standout_motion = tgetflag ("ms");        */
  1468. /*  TF_underscore = tgetflag ("ul");            */
  1469. /*  MagicWrap = tgetflag ("xn");            */
  1470.  
  1471. /*  Get screen size from system, or else from termcap.    */
  1472. /*  get_screen_size (&screen_width, &screen_height);    */
  1473.  
  1474.   FrameRows = FRAME_HEIGHT (selected_frame);
  1475.   FrameCols = FRAME_WIDTH (selected_frame);
  1476.   specified_window = FRAME_HEIGHT (selected_frame);
  1477.  
  1478.   delete_in_insert_mode = 1;
  1479.  
  1480.   UseTabs = 0;
  1481.   scroll_region_ok = 0;
  1482.  
  1483.   /*
  1484.    * Seems to insert lines when it's not supposed to, messing
  1485.    * up the display.  In doing a trace, it didn't seem to be
  1486.    * called much, so I don't think we're losing anything by
  1487.    * turning it off.           (voelker)
  1488.    */
  1489.   line_ins_del_ok = 0;
  1490.   char_ins_del_ok = 1;
  1491.  
  1492.   baud_rate = 19200;
  1493.  
  1494. /*  fast_clear_end_of_line = TS_clr_line != 0;        */
  1495.  
  1496.   FRAME_CAN_HAVE_SCROLL_BARS (selected_frame) = 0;
  1497.   FRAME_HAS_VERTICAL_SCROLL_BARS (selected_frame) = 0;
  1498.  
  1499.   return;
  1500. #endif /* WINDOWSNT */
  1501.  
  1502.   Wcm_clear ();
  1503.   dont_calculate_costs = 0;
  1504.  
  1505.   status = tgetent (buffer, terminal_type);
  1506.   if (status < 0)
  1507.     fatal ("Cannot open termcap database file.\n");
  1508.   if (status == 0)
  1509.     fatal ("Terminal type %s is not defined.\n", terminal_type);
  1510.  
  1511. #ifdef TERMINFO
  1512.   area = (char *) malloc (2044);
  1513. #else
  1514.   area = (char *) malloc (strlen (buffer));
  1515. #endif /* not TERMINFO */
  1516.   if (area == 0)
  1517.     abort ();
  1518.  
  1519.   TS_ins_line = tgetstr ("al", address);
  1520.   TS_ins_multi_lines = tgetstr ("AL", address);
  1521.   TS_bell = tgetstr ("bl", address);
  1522.   BackTab = tgetstr ("bt", address);
  1523.   TS_clr_to_bottom = tgetstr ("cd", address);
  1524.   TS_clr_line = tgetstr ("ce", address);
  1525.   TS_clr_frame = tgetstr ("cl", address);
  1526.   ColPosition = tgetstr ("ch", address);
  1527.   AbsPosition = tgetstr ("cm", address);
  1528.   CR = tgetstr ("cr", address);
  1529.   TS_set_scroll_region = tgetstr ("cs", address);
  1530.   TS_set_scroll_region_1 = tgetstr ("cS", address);
  1531.   RowPosition = tgetstr ("cv", address);
  1532.   TS_del_char = tgetstr ("dc", address);
  1533.   TS_del_multi_chars = tgetstr ("DC", address);
  1534.   TS_del_line = tgetstr ("dl", address);
  1535.   TS_del_multi_lines = tgetstr ("DL", address);
  1536.   TS_delete_mode = tgetstr ("dm", address);
  1537.   TS_end_delete_mode = tgetstr ("ed", address);
  1538.   TS_end_insert_mode = tgetstr ("ei", address);
  1539.   Home = tgetstr ("ho", address);
  1540.   TS_ins_char = tgetstr ("ic", address);
  1541.   TS_ins_multi_chars = tgetstr ("IC", address);
  1542.   TS_insert_mode = tgetstr ("im", address);
  1543.   TS_pad_inserted_char = tgetstr ("ip", address);
  1544.   TS_end_keypad_mode = tgetstr ("ke", address);
  1545.   TS_keypad_mode = tgetstr ("ks", address);
  1546.   LastLine = tgetstr ("ll", address);
  1547.   Right = tgetstr ("nd", address);
  1548.   Down = tgetstr ("do", address);
  1549.   if (!Down)
  1550.     Down = tgetstr ("nl", address); /* Obsolete name for "do" */
  1551. #ifdef VMS
  1552.   /* VMS puts a carriage return before each linefeed,
  1553.      so it is not safe to use linefeeds.  */
  1554.   if (Down && Down[0] == '\n' && Down[1] == '\0')
  1555.     Down = 0;
  1556. #endif /* VMS */
  1557.   if (tgetflag ("bs"))
  1558.     Left = "\b";          /* can't possibly be longer! */
  1559.   else                  /* (Actually, "bs" is obsolete...) */
  1560.     Left = tgetstr ("le", address);
  1561.   if (!Left)
  1562.     Left = tgetstr ("bc", address); /* Obsolete name for "le" */
  1563.   TS_pad_char = tgetstr ("pc", address);
  1564.   TS_repeat = tgetstr ("rp", address);
  1565.   TS_end_standout_mode = tgetstr ("se", address);
  1566.   TS_fwd_scroll = tgetstr ("sf", address);
  1567.   TS_standout_mode = tgetstr ("so", address);
  1568.   TS_rev_scroll = tgetstr ("sr", address);
  1569.   Wcm.cm_tab = tgetstr ("ta", address);
  1570.   TS_end_termcap_modes = tgetstr ("te", address);
  1571.   TS_termcap_modes = tgetstr ("ti", address);
  1572.   Up = tgetstr ("up", address);
  1573.   TS_visible_bell = tgetstr ("vb", address);
  1574.   TS_end_visual_mode = tgetstr ("ve", address);
  1575.   TS_visual_mode = tgetstr ("vs", address);
  1576.   TS_set_window = tgetstr ("wi", address);
  1577.   MultiUp = tgetstr ("UP", address);
  1578.   MultiDown = tgetstr ("DO", address);
  1579.   MultiLeft = tgetstr ("LE", address);
  1580.   MultiRight = tgetstr ("RI", address);
  1581.  
  1582.   AutoWrap = tgetflag ("am");
  1583.   memory_below_frame = tgetflag ("db");
  1584.   TF_hazeltine = tgetflag ("hz");
  1585.   must_write_spaces = tgetflag ("in");
  1586.   meta_key = tgetflag ("km") || tgetflag ("MT");
  1587.   TF_insmode_motion = tgetflag ("mi");
  1588.   TF_standout_motion = tgetflag ("ms");
  1589.   TF_underscore = tgetflag ("ul");
  1590.   MagicWrap = tgetflag ("xn");
  1591.   TF_xs = tgetflag ("xs");
  1592.   TF_teleray = tgetflag ("xt");
  1593.  
  1594.   term_get_fkeys (address);
  1595.  
  1596.   /* Get frame size from system, or else from termcap.  */
  1597.   get_frame_size (&FRAME_WIDTH (selected_frame),
  1598.            &FRAME_HEIGHT (selected_frame));
  1599.   if (FRAME_WIDTH (selected_frame) <= 0)
  1600.     FRAME_WIDTH (selected_frame) = tgetnum ("co");
  1601.   if (FRAME_HEIGHT (selected_frame) <= 0)
  1602.     FRAME_HEIGHT (selected_frame) = tgetnum ("li");
  1603.  
  1604.   min_padding_speed = tgetnum ("pb");
  1605.   TN_standout_width = tgetnum ("sg");
  1606.   TabWidth = tgetnum ("tw");
  1607.  
  1608. #ifdef VMS
  1609.   /* These capabilities commonly use ^J.
  1610.      I don't know why, but sending them on VMS does not work;
  1611.      it causes following spaces to be lost, sometimes.
  1612.      For now, the simplest fix is to avoid using these capabilities ever.  */
  1613.   if (Down && Down[0] == '\n')
  1614.     Down = 0;
  1615. #endif /* VMS */
  1616.  
  1617.   if (!TS_bell)
  1618.     TS_bell = "\07";
  1619.  
  1620.   if (!TS_fwd_scroll)
  1621.     TS_fwd_scroll = Down;
  1622.  
  1623.   PC = TS_pad_char ? *TS_pad_char : 0;
  1624.  
  1625.   if (TabWidth < 0)
  1626.     TabWidth = 8;
  1627.   
  1628. /* Turned off since /etc/termcap seems to have :ta= for most terminals
  1629.    and newer termcap doc does not seem to say there is a default.
  1630.   if (!Wcm.cm_tab)
  1631.     Wcm.cm_tab = "\t";
  1632. */
  1633.  
  1634.   if (TS_standout_mode == 0)
  1635.     {
  1636.       TN_standout_width = tgetnum ("ug");
  1637.       TS_end_standout_mode = tgetstr ("ue", address);
  1638.       TS_standout_mode = tgetstr ("us", address);
  1639.     }
  1640.  
  1641.   if (TF_teleray)
  1642.     {
  1643.       Wcm.cm_tab = 0;
  1644.       /* Teleray: most programs want a space in front of TS_standout_mode,
  1645.        but Emacs can do without it (and give one extra column).  */
  1646.       TS_standout_mode = "\033RD";
  1647.       TN_standout_width = 1;
  1648.       /* But that means we cannot rely on ^M to go to column zero! */
  1649.       CR = 0;
  1650.       /* LF can't be trusted either -- can alter hpos */
  1651.       /* if move at column 0 thru a line with TS_standout_mode */
  1652.       Down = 0;
  1653.     }
  1654.  
  1655.   /* Special handling for certain terminal types known to need it */
  1656.  
  1657.   if (!strcmp (terminal_type, "supdup"))
  1658.     {
  1659.       memory_below_frame = 1;
  1660.       Wcm.cm_losewrap = 1;
  1661.     }
  1662.   if (!strncmp (terminal_type, "c10", 3)
  1663.       || !strcmp (terminal_type, "perq"))
  1664.     {
  1665.       /* Supply a makeshift :wi string.
  1666.      This string is not valid in general since it works only
  1667.      for windows starting at the upper left corner;
  1668.      but that is all Emacs uses.
  1669.  
  1670.      This string works only if the frame is using
  1671.      the top of the video memory, because addressing is memory-relative.
  1672.      So first check the :ti string to see if that is true.
  1673.  
  1674.      It would be simpler if the :wi string could go in the termcap
  1675.      entry, but it can't because it is not fully valid.
  1676.      If it were in the termcap entry, it would confuse other programs.  */
  1677.       if (!TS_set_window)
  1678.     {
  1679.       p = TS_termcap_modes;
  1680.       while (*p && strcmp (p, "\033v  "))
  1681.         p++;
  1682.       if (*p)
  1683.         TS_set_window = "\033v%C %C %C %C ";
  1684.     }
  1685.       /* Termcap entry often fails to have :in: flag */
  1686.       must_write_spaces = 1;
  1687.       /* :ti string typically fails to have \E^G! in it */
  1688.       /* This limits scope of insert-char to one line.  */
  1689.       strcpy (area, TS_termcap_modes);
  1690.       strcat (area, "\033\007!");
  1691.       TS_termcap_modes = area;
  1692.       area += strlen (area) + 1;
  1693.       p = AbsPosition;
  1694.       /* Change all %+ parameters to %C, to handle
  1695.      values above 96 correctly for the C100.  */
  1696.       while (*p)
  1697.     {
  1698.       if (p[0] == '%' && p[1] == '+')
  1699.         p[1] = 'C';
  1700.       p++;
  1701.     }
  1702.     }
  1703.  
  1704.   FrameRows = FRAME_HEIGHT (selected_frame);
  1705.   FrameCols = FRAME_WIDTH (selected_frame);
  1706.   specified_window = FRAME_HEIGHT (selected_frame);
  1707.  
  1708.   if (Wcm_init () == -1)    /* can't do cursor motion */
  1709. #ifdef VMS
  1710.     fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
  1711. It lacks the ability to position the cursor.\n\
  1712. If that is not the actual type of terminal you have, use either the\n\
  1713. DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
  1714. or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.\n",
  1715.            terminal_type);
  1716. #else
  1717.     fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
  1718. It lacks the ability to position the cursor.\n\
  1719. If that is not the actual type of terminal you have,\n\
  1720. use the C-shell command `setenv TERM ...' to specify the correct type.\n\
  1721. It may be necessary to do `unsetenv TERMCAP' as well.\n",
  1722.        terminal_type);
  1723. #endif
  1724.   if (FRAME_HEIGHT (selected_frame) <= 0
  1725.       || FRAME_WIDTH (selected_frame) <= 0)
  1726.     fatal ("The frame size has not been specified.");
  1727.  
  1728.   delete_in_insert_mode
  1729.     = TS_delete_mode && TS_insert_mode
  1730.       && !strcmp (TS_delete_mode, TS_insert_mode);
  1731.  
  1732.   se_is_so = (TS_standout_mode
  1733.           && TS_end_standout_mode
  1734.           && !strcmp (TS_standout_mode, TS_end_standout_mode));
  1735.  
  1736.   /* Remove width of standout marker from usable width of line */
  1737.   if (TN_standout_width > 0)
  1738.     FRAME_WIDTH (selected_frame) -= TN_standout_width;
  1739.  
  1740.   UseTabs = tabs_safe_p () && TabWidth == 8;
  1741.  
  1742.   scroll_region_ok
  1743.     = (Wcm.cm_abs
  1744.        && (TS_set_window || TS_set_scroll_region || TS_set_scroll_region_1));
  1745.  
  1746.   line_ins_del_ok = (((TS_ins_line || TS_ins_multi_lines)
  1747.               && (TS_del_line || TS_del_multi_lines))
  1748.              || (scroll_region_ok && TS_fwd_scroll && TS_rev_scroll));
  1749.  
  1750.   char_ins_del_ok = ((TS_ins_char || TS_insert_mode
  1751.               || TS_pad_inserted_char || TS_ins_multi_chars)
  1752.              && (TS_del_char || TS_del_multi_chars));
  1753.  
  1754.   fast_clear_end_of_line = TS_clr_line != 0;
  1755.  
  1756.   init_baud_rate ();
  1757.   if (read_socket_hook)        /* Baudrate is somewhat */
  1758.                 /* meaningless in this case */
  1759.     baud_rate = 9600;
  1760.  
  1761.   FRAME_CAN_HAVE_SCROLL_BARS (selected_frame) = 0;
  1762.   FRAME_HAS_VERTICAL_SCROLL_BARS (selected_frame) = 0;
  1763. }
  1764.  
  1765. /* VARARGS 1 */
  1766. _VOID_ _VARARGS_
  1767. #ifdef USE_PROTOTYPES
  1768. #include <stdarg.h>
  1769. fatal (char *str, ...)
  1770. #else
  1771. fatal (str, arg1, arg2)
  1772.      char *str, *arg1, *arg2;
  1773. #endif    
  1774. {
  1775. #ifdef USE_PROTOTYPES
  1776.   va_list args;
  1777.  
  1778.   fprintf (stderr, "emacs: ");
  1779.   va_start(args, str);
  1780.   vfprintf (stderr, str, args);
  1781.   va_end(args);
  1782. #else  
  1783.   fprintf (stderr, "emacs: ");
  1784.   fprintf (stderr, str, arg1, arg2);
  1785. #endif
  1786.   fflush (stderr);
  1787.   exit (1);
  1788. }
  1789.